package cn.mill.demo.api;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import cn.mill.spring.annotation.MillScan;

@SpringBootApplication
@ComponentScan({ "cn.mill.demo.api", "cn.mill.demo.api.controller" }) // 扫描包
@MillScan({ "cn.mill.demo.postcode", "cn.mill.demo.weixin", "cn.mill.demo.open" })
@EnableAsync
public class SpringBootDemoApplication {
	private static Logger logger = LoggerFactory.getLogger(SpringBootDemoApplication.class);
	
	public static void main(String[] args) {
		logger.debug("----------------start---------------------------");
		ApplicationContext app = SpringApplication.run(SpringBootDemoApplication.class, args);
		logger.debug("----------------end  ---------------------------");
	}
	
	@Bean(name = "localeResolver")
	public LocaleResolver localeResolver() {
		CookieLocaleResolver slr = new CookieLocaleResolver();
		// 默认语言
		slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
		slr.setCookieName("Language");
		return slr;
	}
	
	/**
	 * 
	 * 限定URL后缀
	 */
	@Bean
	public ServletRegistrationBean<DispatcherServlet> dispatcherRegistration(DispatcherServlet dispatcherServlet) {
		ServletRegistrationBean<DispatcherServlet> reg = new ServletRegistrationBean<DispatcherServlet>(dispatcherServlet);
		reg.addUrlMappings("*.do");
		reg.addUrlMappings("*.html");
		reg.addUrlMappings("*.shtml");
		reg.addUrlMappings("*.css");
		reg.addUrlMappings("*.js");
		reg.addUrlMappings("*.png");
		reg.addUrlMappings("*.gif");
		reg.addUrlMappings("*.ico");
		reg.addUrlMappings("*.jpeg");
		reg.addUrlMappings("*.jpg");
		return reg;
	}
	
}
